<?php

declare(strict_types=1);

namespace App\Controller;

use App\JsonRpc\RpcDemoServiceInterface;
use App\JsonRpc\RpcOrderServiceInterface;
use App\JsonRpc\RpcProductServiceInterface;
use Hyperf\Di\Annotation\Inject;

class IndexController extends AbstractController
{
    #[Inject]
    protected RpcDemoServiceInterface $rpcDemoService;
    #[Inject]
    protected RpcOrderServiceInterface $rpcOrderService;
    #[Inject]
    protected RpcProductServiceInterface $rpcProductService;

    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

    public function health()
    {
        $demo = '';
        try {
            $demo = $this->rpcDemoService->health(__FILE__);
        } catch (\Throwable $th) {
            $demo = $th->getMessage();
        }
        $order = '';
        try {
            $order = $this->rpcOrderService->health(__FILE__);
        } catch (\Throwable $th) {
            $order = $th->getMessage();
        }
        $product = '';
        try {
            $product = $this->rpcProductService->health(__FILE__);
        } catch (\Throwable $th) {
            $product = $th->getMessage();
        }

        return [
            'demo' => $demo,
            'order' => $order,
            'product' => $product,
        ];
    }

    public function test()
    {
        $demo = null;
        try {
            $demo = $this->rpcDemoService->router('TestService', 'test', [__LINE__]);
        } catch (\Throwable $th) {
            $demo = $th->getMessage();
        }
        $order = null;
        try {
            $order = $this->rpcOrderService->router('TestService', 'test', [__LINE__]);
        } catch (\Throwable $th) {
            $order = $th->getMessage();
        }
        $product = null;
        try {
            $product = $this->rpcProductService->router('TestService', 'test', [__LINE__]);
        } catch (\Throwable $th) {
            $product = $th->getMessage();
        }

        return [
            'demo' => $demo,
            'order' => $order,
            'product' => $product,
        ];
    }
}
